import 'package:flutter/cupertino.dart';

//自定义缓存页面部件
class KeepAliveWrap extends StatefulWidget {
  final Widget child;
  final bool keepAlive;
  const KeepAliveWrap({super.key, required this.child, this.keepAlive = true});

  @override
  State<KeepAliveWrap> createState() => _KeepAliveWrapState();
}

class _KeepAliveWrapState extends State<KeepAliveWrap>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child;
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => widget.keepAlive;

  //Widget更新时调用
  @override
  void didUpdateWidget(covariant KeepAliveWrap oldWidget) {
    // TODO: implement didUpdateWidget
    if (oldWidget.keepAlive != widget.keepAlive) {
      updateKeepAlive();
    }
    super.didUpdateWidget(oldWidget);
  }
}
